package com.ruoyi.hospital.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.hospital.domain.YlArea;
import com.ruoyi.hospital.domain.YlDepartment;
import com.ruoyi.hospital.domain.YlDoctor;
import com.ruoyi.hospital.domain.YlLabel;
import com.ruoyi.hospital.service.IYlAreaService;
import com.ruoyi.hospital.service.IYlDepartmentService;
import com.ruoyi.hospital.service.IYlDoctorService;
import com.ruoyi.hospital.service.IYlLabelService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.YlTitleControl;
import com.ruoyi.system.service.IYlTitleControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 医生信息Controller
 * 
 * @author ruoyi
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/doctor")
public class YlDoctorController extends BaseController
{
    @Autowired
    private IYlDoctorService ylDoctorService;

    @Autowired
    private IYlAreaService ylAreaService;

//    @Autowired
//    private IYlTitleControlService ylTitleControlService;

    @Autowired
    private IYlDepartmentService ylDepartmentService;

    @Autowired
    private IYlLabelService ylLabelService;

    /**
     * 查询医生信息列表
     */
    @RequiresPermissions("hospital:doctor:list")
    @GetMapping("/list")
    public TableDataInfo list(YlDoctor ylDoctor)
    {
        startPage();
        long startTime = System.currentTimeMillis(); //获取开始时间
        List<YlDoctor> list = ylDoctorService.selectYlDoctorList(ylDoctor);
        for (int i = 0; i < list.size(); i++) {
//            list.get(i).setTitleName(ylTitleControlService.selectYlTitleControlByTitleId(list.get(i).getTitleId()).getTitleName());

            list.get(i).setAreaName(ylAreaService.selectYlAreaByAreaId(list.get(i).getAreaId()).getName());

//            YlTitleControl ylTitleControl = new YlTitleControl();
//            ylTitleControl.setTitleName(ylTitleControlService.selectYlTitleControlByTitleId(list.get(i).getTitleId()).getTitleName());
//            list.get(i).setYlTitleControll(ylTitleControl);

            YlArea ylArea = new YlArea();
            ylArea.setName(ylAreaService.selectYlAreaByAreaId(list.get(i).getAreaId()).getName());
            list.get(i).setYlArea(ylArea);

            YlDepartment ylDepartment = new YlDepartment();
            ylDepartment.setSuperId(ylDepartmentService.selectYlDepartmentByDepartmentId(list.get(i).getDepartmentId()).getSuperId());
            ylDepartment.setName(ylDepartmentService.selectYlDepartmentByDepartmentId(list.get(i).getDepartmentId()).getName());
            if(ylDepartment.getSuperId()!=0){
                ylDepartment.setSuperName(ylDepartmentService.selectYlDepartmentByDepartmentId(ylDepartment.getSuperId()).getName());
            } else {
                ylDepartment.setSuperName(ylDepartment.getName());
            }
            list.get(i).setYlDepartment(ylDepartment);

            String[] arr = list.get(i).getLabelIds().split(",");

            YlDoctor newDoctor = new YlDoctor();

            String[] newArr = new String[arr.length];
            for (int j = 0; j < arr.length; j++) {
                newArr[j] = ylLabelService.selectYlLabelByLabelId((long) Integer.parseInt(arr[j])).getName();
                newDoctor.setYlLabelArr(newArr);
            }
            list.get(i).setYlLabelArr(newDoctor.getYlLabelArr());
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间
        return getDataTable(list);
    }

    /**
     * 导出医生信息列表
     */
    @RequiresPermissions("hospital:doctor:export")
    @Log(title = "医生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YlDoctor ylDoctor)
    {
        List<YlDoctor> list = ylDoctorService.selectYlDoctorList(ylDoctor);
        ExcelUtil<YlDoctor> util = new ExcelUtil<YlDoctor>(YlDoctor.class);
        util.exportExcel(response, list, "医生信息数据");
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("hospital:doctor:edit")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeState")
    public AjaxResult changeState(@RequestBody YlDoctor ylDoctor)
    {
        return toAjax(ylDoctorService.updateDoctorState(ylDoctor));
    }

    /**
     * 查询所有医生信息
     */
    @GetMapping("/doctorlist")
    public TableDataInfo getList()
    {
        List<YlDoctor> ylDoctorList = ylDoctorService.selectAll();
        return getDataTable(ylDoctorList);
    }

    /**
     * 获取医生信息详细信息
     */
    @RequiresPermissions("hospital:doctor:query")
    @GetMapping(value = "/{doctorId}")
    public AjaxResult getInfo(@PathVariable("doctorId") Long doctorId)
    {
        long startTime = System.currentTimeMillis(); //获取开始时间
        YlDoctor ylDoctor = ylDoctorService.selectYlDoctorByDoctorId(doctorId);
        String[] split = ylDoctor.getLabelIds().split(",");
        String[] newArr = new String[split.length];
        for(int i = 0; i < split.length; i++) {
            newArr[i] = ylLabelService.selectYlLabelByLabelId((long)Integer.parseInt(split[i])).getName();
        }
        ylDoctor.setYlLabelArr(newArr);

        List<YlLabel> ylLabelList = new ArrayList<>();
        ylLabelList.addAll(ylLabelService.selectAll());
        ylDoctor.setYlLabelList(ylLabelList);

        ylDoctor.setOldLabelIds(split);
        ylDoctor.setYlAreaList(ylAreaService.selectAll());
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间
        return AjaxResult.success(ylDoctor);
    }

    /**
     * 新增医生信息
     */
    @RequiresPermissions("hospital:doctor:add")
    @Log(title = "医生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YlDoctor ylDoctor)
    {
        return toAjax(ylDoctorService.insertYlDoctor(ylDoctor));
    }

    /**
     * 修改医生信息
     */
    @RequiresPermissions("hospital:doctor:edit")
    @Log(title = "医生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YlDoctor ylDoctor)
    {
        return toAjax(ylDoctorService.updateYlDoctor(ylDoctor));
    }

    /**
     * 删除医生信息
     */
    @RequiresPermissions("hospital:doctor:remove")
    @Log(title = "医生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{doctorIds}")
    public AjaxResult remove(@PathVariable Long[] doctorIds)
    {
        return toAjax(ylDoctorService.deleteYlDoctorByDoctorIds(doctorIds));
    }
}
